/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdServicio.java
 *
 * Created on 12/02/2011, 01:22:07 AM
 */

package com.fsl.sicotec.UI;

import java.util.ArrayList;
import com.fsl.sicotec.comun.Entidades.Utiles;
import com.fsl.sicotec.Logica.LoServicio;
import com.fsl.sicotec.comun.Entidades.Servicio;

/**
 *
 * @author User
 */
public class jdServicio extends javax.swing.JDialog {
private Utiles uti=null;
      private Servicio objservicio = null;
    private LoServicio loservicio = null;
    private ArrayList<Servicio> lista;
    private String codigo = "";
    /** Creates new form jdServicio */
    public jdServicio(jdListadoServicios parent, boolean modal,int codServi) {

        super(parent, modal);
        initComponents();
        txtDescripcion.requestFocusInWindow();
       this.setTitle("Actualizar Cliente");
    codigo = String.valueOf(codServi);
        txtCodigo.setText(String.valueOf(codigo));
        mostarServicio(codServi);
    }
      private void mostarServicio(int codServicio) {
        loservicio = new LoServicio();
        lista = (ArrayList<Servicio>) loservicio.mostrarServicioCodigo(codServicio);
        txtDescripcion.setText(lista.get(0).getVdescripcion());
        txtPU.setText(String.valueOf(lista.get(0).getDprecio()));
      }

    public jdServicio(jdListadoServicios parent,boolean modal) {
       super(parent, modal);
        initComponents();
          txtDescripcion.requestFocusInWindow();

    }

  

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        txtPU = new javax.swing.JTextField();
        btnAceptar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Mantenimiento de Servicio");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de servicio"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Código");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));
        jLabel1.getAccessibleContext().setAccessibleName("Servicio");

        txtCodigo.setEnabled(false);
        jPanel1.add(txtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 20, 90, -1));

        jLabel2.setText("Descripción");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, -1, -1));

        jLabel3.setText("P.U.");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));
        jPanel1.add(txtDescripcion, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 50, 350, -1));
        jPanel1.add(txtPU, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, 90, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 450, 130));

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 150, -1, -1));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 150, 90, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
       setVisible(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:

          if (!txtDescripcion.getText().equals("") & !txtPU.getText().equals("")) {
            objservicio = new Servicio();
            //Pasando los datos a la entidad

        objservicio.setVdescripcion(txtDescripcion.getText().trim().toUpperCase() );
        objservicio.setDprecio(Float.parseFloat(txtPU.getText().trim()));
        objservicio.setBestado(1);
        objservicio.setVaccion("INSERTAR");
        objservicio.setVusuario_creacion(uti.getUsuario());
        objservicio.setVusuario_control(uti.getUsuario());
        objservicio.setVestacion(uti.getTerminal());
        //Ejecutando el metodo

            try {
                loservicio = new LoServicio();
                if (codigo.length() == 0) {
                    loservicio.registrarServicio(objservicio);

                } else {
                      objservicio.setVaccion("ACTUALIZAR");
                    int resultmsg = javax.swing.JOptionPane.showConfirmDialog(null, "¿Esta seguro de actualizar los datos del servicio:?"  );
                    if (resultmsg == 0) {
                        objservicio.setCod_servicio(Integer.parseInt(txtCodigo.getText().trim()));
                        loservicio.actualizarServicio(objservicio);
                        javax.swing.JOptionPane.showMessageDialog(null, "Datos actualizados correctamente");
                    }

                }
                setVisible(false);
            } catch (Exception e) {
                javax.swing.JOptionPane.showMessageDialog(null, e.getMessage());
            }

        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "Ingrese todos los datos para el Registro del Cliente");
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                jdServicio dialog = new jdServicio(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtPU;
    // End of variables declaration//GEN-END:variables

}
